package cn.broadsense.biz.service.impl;

import cn.broadsense.biz.domain.Channel;
import cn.broadsense.biz.domain.ChannelOrg;
import cn.broadsense.biz.domain.CollectDataTaskChannel;
import cn.broadsense.biz.mapper.CollectDataTaskChannelMapper;
import cn.broadsense.biz.service.ChannelOrgService;
import cn.broadsense.biz.service.ChannelService;
import cn.broadsense.biz.service.CollectDataTaskChannelService;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author pengxin
 * @description 针对表【t_collect_data_task_channel(采集任务点位列表)】的数据库操作Service实现
 * @createDate 2023-09-12 10:55:35
 */
@Service
public class CollectDataTaskChannelServiceImpl extends ServiceImpl<CollectDataTaskChannelMapper, CollectDataTaskChannel>
        implements CollectDataTaskChannelService {

    @Lazy
    @Resource
    private ChannelService channelService;

    @Lazy
    @Resource
    private ChannelOrgService channelOrgService;

    @Override
    public List<CollectDataTaskChannel> listByTaskId(Long taskId) {
        LambdaQueryWrapper<CollectDataTaskChannel> query = new LambdaQueryWrapper<>();
        query.eq(CollectDataTaskChannel::getCollectDataTaskId, taskId);
        List<CollectDataTaskChannel> list = list(query);
        if (CollUtil.isNotEmpty(list)) {
            List<Long> ids = list.stream().map(CollectDataTaskChannel::getChannelId).collect(Collectors.toList());
            List<Channel> channelList = channelService.listByIds(ids);
            if (CollUtil.isNotEmpty(channelList)) {
                Map<Long, Channel> channelMap = channelList.stream().collect(Collectors.toMap(Channel::getId, e -> e));
                Set<String> orgCodeSet = channelList.stream().map(Channel::getOrgCode).collect(Collectors.toSet());
                LambdaQueryWrapper<ChannelOrg> queryCode = new LambdaQueryWrapper<>();
                queryCode.in(ChannelOrg::getOrgCode, orgCodeSet);
                //组装父级编码用户前端树结构回显
                List<ChannelOrg> orgList = channelOrgService.list(queryCode);
                Map<String, String> orgCodeMap = new HashMap<>(orgList.size());
                if (CollUtil.isNotEmpty(orgList)) {
                    orgCodeMap = orgList.stream().collect(Collectors.toMap(ChannelOrg::getOrgCode, e -> e.getParentOrgCode()));
                }
                Map<String, String> finalOrgCodeMap = orgCodeMap;
                list.stream().forEach(e -> {
                    Channel channel = channelMap.get(e.getChannelId());
                    e.setParentOrgCode(finalOrgCodeMap.get(channel.getOrgCode()));
                    e.setChannel(channel);
                });
            }
        }
        return list;
    }

    @Override
    public CollectDataTaskChannel getById(Long id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public List<CollectDataTaskChannel> getListByCollectDataTaskId(Long collectDataTaskId) {
        LambdaQueryWrapper<CollectDataTaskChannel> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CollectDataTaskChannel::getCollectDataTaskId, collectDataTaskId);
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public CollectDataTaskChannel getByChannelIdAndCollectDataTaskId(Long channelId, Long collectDataTaskId) {
        LambdaQueryWrapper<CollectDataTaskChannel> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CollectDataTaskChannel::getChannelId, channelId);
        queryWrapper.eq(CollectDataTaskChannel::getCollectDataTaskId, collectDataTaskId);
        queryWrapper.last("limit 1");
        return this.baseMapper.selectOne(queryWrapper);
    }

    @Override
    public List<Long> taskIdsSearchChannel(String channelName, String channelSn) {
        if (StrUtil.isBlank(channelName) && StrUtil.isBlank(channelSn)) {
            return null;
        }
        LambdaQueryWrapper<CollectDataTaskChannel> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(CollectDataTaskChannel::getCollectDataTaskId);
        if (StrUtil.isNotBlank(channelName)) {
            queryWrapper.like(CollectDataTaskChannel::getChannelName, channelName);
        }
        if (StrUtil.isNotBlank(channelSn)) {
            queryWrapper.like(CollectDataTaskChannel::getChannelSn, channelSn);
        }
        List<CollectDataTaskChannel> dataTaskChannelList = this.baseMapper.selectList(queryWrapper);
        if (CollUtil.isNotEmpty(dataTaskChannelList)) {
            Set<Long> collect = dataTaskChannelList.stream().map(CollectDataTaskChannel::getCollectDataTaskId).collect(Collectors.toSet());
            return CollUtil.newArrayList(collect);
        }
        return null;
    }

}




